package cn.ym.mall.service.impl;

import cn.ym.mall.dao.GradeDao;
import cn.ym.mall.dao.impl.GradeDaoImpl;
import cn.ym.mall.entity.Grade;
import cn.ym.mall.service.GradeService;
import cn.ym.mall.utils.DruidUtils;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class GradeServiceImpl implements GradeService {
    private GradeDao gradeDao = new GradeDaoImpl();
    /**
     * 根据会员id查询折扣
     *
     * @param grid
     * @return
     */
    @Override
    public Grade getGradeBygrid(Integer grid) {
        Connection conn = null;
        Grade grade = null;
        try {
            conn = DruidUtils.getConnection();
            grade = gradeDao.getGradeBygrid(grid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return grade;
    }

    /**
     * 查询会员等级
     *
     * @return
     */
    @Override
    public List<Grade> getAllGrade() {
        Connection conn = null;
        List<Grade> gradeList = new ArrayList<>();
        try{
            conn = DruidUtils.getConnection();
            gradeList = gradeDao.getAllGrade();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return gradeList;
    }

    /**
     * 修改会员折扣
     *
     * @param grade
     * @return
     */
    @Override
    public boolean updateGrade(Grade grade) {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if(gradeDao.updateGrade(grade) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 通过grid删除会员
     *
     * @param grid
     * @return
     */
    @Override
    public boolean deleteGradeBygrid(Integer grid) {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if(gradeDao.deleteGradeBygrid(grid) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }
}
